from typing import Any

import requests


BASE_URL = "http://127.0.0.1:8002/xiaozhi"
TAG = __name__
CHAT_URL = f"{BASE_URL}/chat/chat"
MEM_URL = f"{BASE_URL}/chat/memory"
json_headers = {"content-type": "application/json"}


def inner_send_chat(chat_history_id, mac_address, face_id, role, content, prompt_tokens, total_tokens, completion_tokens, prompt_ms, total_ms, completion_ms, sequence, logger):
    payload = {
        "chatHistoryId": chat_history_id,
        "macAddress": mac_address,
        "faceId": face_id,
        "role": role,
        "content": content,
        "promptTokens": prompt_tokens,
        "totalTokens": total_tokens,
        "completionTokens": completion_tokens,
        "promptMs": prompt_ms,
        "totalMs": total_ms,
        "completionMs": completion_ms,
        "sequence": sequence,
    }

    logger.bind(tag=TAG).info(f"发送聊天请求: {payload}")

    response = requests.post(CHAT_URL, json=payload, headers=json_headers).json()

    logger.bind(tag=TAG).info(f"发送聊天结果: {response}")

    if response['code'] == 0:
        return response['data']

    return None
def inner_send_mem(mem_id, chat_id, mem_result,logger):
    payload = {
        "id": mem_id,
        "chat_id": chat_id,
        "mem_result": mem_result,
    }

    logger.bind(tag=TAG).info(f"发送记忆请求: {payload}")

    response = requests.post(MEM_URL, json=payload, headers=json_headers).json()

    logger.bind(tag=TAG).info(f"发送记忆结果: {response}")

    if response['code'] == 0:
        return response['data']

    return None